﻿/* This Component Library is licensed under the GNU General Public License version 2 (GPLv2) 
 * You can use this software and its associated files as you wish so long as derived versions
 * or works based on this library are also freely released.
 * If you require a commercial licence for this software without the above restrictions please
 * contact me via scott_blood@hotmail.com or via CodePlex.
 * (c) 2010 Scott Blood
 * Visit SGBControls.CodePlex.com for the latest version of this library.
 */ 
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace SGBControls.Editors
{
    [ToolboxItem(false)]
    public class SGBBaseControl : UserControl
    {
        protected internal Color fBorderColor = SystemColors.WindowFrame;
        protected internal BorderStyles fBorderStyle = BorderStyles.Flat;
        protected internal SGBEditStyleController fStyleController = null;
        protected internal Color fActiveBackgroundColor = SystemColors.Info;
        protected internal Color fActiveForeColor = SystemColors.InfoText;
        protected internal Color fInactiveBackgroundColor = SystemColors.Window;
        protected internal Color fInactiveForecolor = SystemColors.WindowText;
        
        public event EventHandler BorderStyleChanged;

        public SGBBaseControl()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        [Description("Gets or sets the background color used when an editor control has the focus")]
        public Color ActiveBackgroundColor
        {
            get
            {
                return fActiveBackgroundColor;
            }
            set
            {
                fActiveBackgroundColor = value;
            }
        }

        [Description("Gets or sets the Forecolor used when an editor control has the focus")]
        public Color ActiveForeColor
        {
            get
            {
                return fActiveForeColor;
            }
            set
            {
                fActiveForeColor = value;
            }
        }

        [Description("Gets or sets the background color used when an editor control does not have the focus")]
        public Color InActiveBackgroundColor
        {
            get
            {
                return fInactiveBackgroundColor;
            }
            set
            {
                fInactiveBackgroundColor = value;
            }
        }

        [Description("Gets or sets the Forecolor used when an editor control does not have the focus")]
        public Color InActiveForeColor
        {
            get
            {
                return fInactiveForecolor;
            }
            set
            {
                fInactiveForecolor = value;
            }
        }

        [Description("Gets or sets the color used to draw the controls border")]
        public Color BorderColor
        {
            get
            {
                return this.fBorderColor;
            }
            set
            {
                if (this.fBorderColor != value)
                {
                    this.fBorderColor = value;
                    Invalidate();
                }
            }
        }

        [Description("Gets or sets the edit controller used for the Editor Control")]
        public SGBEditStyleController StyleController
        {
            get
            {
                return fStyleController;
            }
            set
            {
                if (fStyleController != value)
                {
                    fStyleController = value;
                    if (fStyleController != null)
                    {
                        fStyleController.Invalidated += new EventHandler(fStyleController_Invalidated);
                    }
                    Invalidate();
                }
            }
        }

        void fStyleController_Invalidated(object sender, EventArgs e)
        {
            Invalidate();
        }

        [Description("Gets or sets the style used to draw borders around the Editor Control.")]
        public new BorderStyles BorderStyle
        {
            get
            {
                return fBorderStyle;
            }
            set
            {
                if (fBorderStyle != value)
                {
                    fBorderStyle = value;
                    if (BorderStyleChanged != null)
                        BorderStyleChanged(this, new EventArgs());
                    Invalidate();
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (fStyleController != null)
            {
                ControlPainter Painter = new ControlPainter(e.Graphics, ClientRectangle, fStyleController);
                Painter.Draw();
            }
            else
            {
                SGBEditStyleController StyleController = new SGBEditStyleController();
                StyleController.BorderColor = fBorderColor;
                StyleController.BorderStyle = fBorderStyle;
                ControlPainter Painter = new ControlPainter(e.Graphics, ClientRectangle, StyleController);
                Painter.Draw();
                StyleController.Dispose();
            }
        }

        #region Hide Unwanted properties

        [Browsable(false)]
        public new Size AutoScrollMargin
        {
            get
            {
                return base.AutoScrollMargin;
            }
            set
            {
                base.AutoScrollMargin = value;
            }
        }

        [Browsable(false)]
        public new Size AutoScrollMinSize
        {
            get
            {
                return base.AutoScrollMargin;
            }
            set
            {
                base.AutoScrollMargin = value;
            }
        }

        [Browsable(false)]
        public new AutoSizeMode AutoSizeMode
        {
            get
            {
                return base.AutoSizeMode;
            }
            set
            {
                base.AutoSizeMode = value;
            }
        }

        [Browsable(false)]
        public override AutoValidate AutoValidate
        {
            get
            {
                return base.AutoValidate;
            }
            set
            {
                base.AutoValidate = value;
            }
        }

        [Browsable(false)]
        public override bool AutoScroll
        {
            get
            {
                return base.AutoScroll;
            }
            set
            {
                base.AutoScroll = value;
            }
        }

        [Browsable(false)]
        public override Point AutoScrollOffset
        {
            get
            {
                return base.AutoScrollOffset;
            }
            set
            {
                base.AutoScrollOffset = value;
            }
        }

        [Browsable(false)]
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }
            set
            {
                base.AutoSize = value;
            }
        }

        [Browsable(false)]
        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {                
                base.BackgroundImage = value;
            }
        }

        [Browsable(false)]
        public override ImageLayout BackgroundImageLayout
        {
            get
            {
                return base.BackgroundImageLayout;
            }
            set
            {
                base.BackgroundImageLayout = value;
            }
        }

        public virtual object GetNativeControl()
        {
            return null;
        }

        #endregion
    }
}
